package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.AddressMapper;
import cn.scxy.bookcity.pojo.dto.AddressDTO;
import cn.scxy.bookcity.pojo.vo.AddressListItemVO;
import cn.scxy.bookcity.pojo.vo.AddressStandardVO;
import cn.scxy.bookcity.service.IShippingAddressService;
import cn.scxy.bookcity.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ShippingAddressServiceImpl implements IShippingAddressService {
    @Autowired
    AddressMapper addressMapper;
    @Override
    public void insert(AddressDTO addressDTO) {
        log.debug("开始处理添加收货地址业务,参数:{}", addressDTO);
        int i = addressMapper.insert(addressDTO);
        if(i==0){
            String message = "服务器繁忙,添加失败";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        log.debug("添加成功");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理删除收货地址业务,参数:{}", id);
        int i = addressMapper.deleteById(id);
        if(i==0){
            String message = "服务器繁忙,删除失败";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        log.debug("删除成功");
    }

    @Override
    public void update(AddressDTO addressDTO) {
        log.debug("开始处理更新收货地址业务,参数:{}", addressDTO);
        int i = addressMapper.update(addressDTO);
        if(i==0){
            String message = "服务器繁忙,更新失败";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        log.debug("更新成功");
    }

    @Override
    public AddressStandardVO getStandardById(Long id) {
        return addressMapper.getStandardById(id);
    }

    @Override
    public List<AddressListItemVO> listByuId(Long uId) {
        return addressMapper.listByuId(uId);
    }


}
